package com.ht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.domain.bo.TbScheduleBO;
import com.ht.domain.po.*;
import com.ht.mapper.*;
import com.ht.service.ScheduleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private TbDoctorMapper tbDoctorMapper;

    @Autowired
    private TbDoctorLevelMapper tbDoctorLevelMapper;

    @Autowired
    private TbLevelMapper tbLevelMapper;

    @Autowired
    private TbHospitalMapper tbHospitalMapper;

    @Override
    public TbScheduleBO queryByDate(String format) {
        List<TbSchedulePO> list = scheduleMapper.queryByDate(format);
        TbScheduleBO bo = new TbScheduleBO();
        int i = 0;
        for (TbSchedulePO po : list) {
            i += po.getNum();
        }
        bo.setAllNum(i);
        return bo;
    }

    @Override
    public List<TbScheduleBO> queryPublicById(Long id, String format) {
        List<TbSchedulePO> list = scheduleMapper.queryPublicById(id, format);
        List<TbScheduleBO> boList = new ArrayList<>();
        for (TbSchedulePO po : list) {
            TbScheduleBO bo = new TbScheduleBO();
            //根据科室id查询所属科室
            TbDepartment dpo = departmentMapper.queryById(id);
            //把当前ID所对应的科室名放入TbScheduleBO 中
            bo.setDepartmentName(dpo.getDepartmentName());
            //根据医生id查医生所对字段
            QueryWrapper q = new QueryWrapper();
            q.eq("id",po.getDoctorId());
//            TbDoctor tbDoctorPO = tbDoctorMapper.queryById(po.getDoctorId());
            TbDoctor tbDoctorPO = tbDoctorMapper.selectOne(q);
            //把当前ID所对应的医生信息放入TbScheduleBO 中
            bo.setDoctorName(tbDoctorPO.getDoctorName());
            bo.setDoctorResume(tbDoctorPO.getResume());
            bo.setDoctorScore(tbDoctorPO.getScore());
            bo.setSickNum(tbDoctorPO.getSickNum());
            //根据医院ID查询医院名称
            TbHospital tbHospital = tbHospitalMapper.selectById(tbDoctorPO.getHospitalId());
            bo.setDoctorHosital(tbHospital.getHospitalName());
            BeanUtils.copyProperties(po,bo);
            //判断上午/下午/晚上挂号状态
            if(bo.getDoctorId() == null || "".equals(bo.getDoctorId())){
                bo.setStatus("停诊");
            }
            if (bo.getNum() == 0){
                bo.setStatus("号已挂完");
            }
            if (bo.getNum() > 0){
                bo.setStatus("挂号");
            }
            //根据医生ID查询医生职位
            QueryWrapper<TbDoctorLevel> qq = new QueryWrapper();
            qq.eq("doctor_id",po.getDoctorId());
            TbDoctorLevel one = tbDoctorLevelMapper.selectOne(qq);
            TbLevel tbLevelPO = tbLevelMapper.selectById(one.getLevelId());
            bo.setDoctorLevel(tbLevelPO.getLevel());
            boList.add(bo);
        }
        return boList;
    }

    @Override
    public  List<TbScheduleBO> queryAll() {
        List<TbSchedulePO> list = scheduleMapper.queryAll();
        System.out.println(list.size());
        System.out.println(list.size());
        System.out.println(list.size());
        System.out.println(list.size());
        List<TbScheduleBO> BoList = new ArrayList<>();
        for (TbSchedulePO po : list) {
            TbScheduleBO bo = new TbScheduleBO();
            BeanUtils.copyProperties(po,bo);
            BoList.add(bo);
        }
        return BoList;
    }


}
